﻿using System.Linq;
using System.Windows.Forms;
using InstantMessenger.Properties;
using InstantMessenger.Transports;
using TweetSharp;

namespace InstantMessenger
{
    public partial class AddEditContactForm : Form
    {
        private Contact contact;
        private string accessToken;
        private string accessTokenSecret;
        private OAuthRequestToken requestToken;

        public AddEditContactForm() : this(null)
        {
        }

        public AddEditContactForm(Contact contact)
        {
            this.contact = contact;
            InitializeComponent();

            if (contact != null)
            {
                nameTextBox.Text = contact.Name;
                nameTextBox.Enabled = false;
                
                var twitterContact = Settings.Default.TwitterSettings.GetTwitterContact(contact.Name);
                if (twitterContact != null)
                {
                    twitterUsernameTextBox.Text = twitterContact.TwitterUserName;
                    var isAuthorised = twitterContact.IsAuthorised;

                    twitterAuthoriseButton.Visible = !isAuthorised;
                    twitterAuthCodeTextBox.Visible = !isAuthorised;
                    twitterAuthDoneButton.Visible = !isAuthorised;
                    twitterAuthorisationCodeLabel.Visible = !isAuthorised;
                    twitterAuthorisedLabel.Visible = isAuthorised;

                    accessToken = twitterContact.AccessToken;
                    accessTokenSecret = twitterContact.AccessTokenSecret;
                }
            }
        }

        private void okButton_Click(object sender, System.EventArgs e)
        {
            var name = nameTextBox.Text;
            if (contact == null)
            {
                if (Settings.Default.ContactNames.Cast<string>().Any(x => x == name))
                {
                    MessageBox.Show(this, "Contact already exists!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                contact = new Contact(name);

                Settings.Default.ContactNames.Add(name);
                Settings.Default.Save();
            }

            if (twitterUsernameTextBox.Text != null)
            {
                var twitterSettings = Settings.Default.TwitterSettings;
                var twitterContact = twitterSettings.TwitterContacts.FirstOrDefault(x => x.Name == name);
                if (twitterContact == null)
                {
                    twitterContact = new TwitterContact(name, twitterUsernameTextBox.Text)
                                         {
                                             AccessToken = accessToken,
                                             AccessTokenSecret = accessTokenSecret
                                         };

                    twitterSettings.TwitterContacts.Add(twitterContact);
                }

                if (!twitterContact.IsAuthorised)
                {
                    twitterContact.AccessToken = accessToken;
                    twitterContact.AccessTokenSecret = accessTokenSecret;
                }

                Settings.Default.Save();
            }

            Close();
        }

        private void twitterUsernameTextBox_TextChanged(object sender, System.EventArgs e)
        {
            twitterAuthoriseButton.Enabled = !string.IsNullOrEmpty(twitterUsernameTextBox.Text);
        }

        private void twitterAuthoriseButton_Click(object sender, System.EventArgs e)
        {
            twitterAuthoriseButton.Enabled = false;
            requestToken = TwitterTransport.StartAuthorise();
        }

        private void twitterAuthDoneButton_Click(object sender, System.EventArgs e)
        {
            if (string.IsNullOrEmpty(twitterAuthCodeTextBox.Text))
            {
                MessageBox.Show("Enter code!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            var token = TwitterTransport.EndAuthorise(requestToken, twitterAuthCodeTextBox.Text);
            accessToken = token.Token;
            accessTokenSecret = token.TokenSecret;

            twitterAuthorisedLabel.Visible = true;
            twitterAuthDoneButton.Visible = false;
            twitterAuthoriseButton.Visible = false;
            twitterAuthCodeTextBox.Visible = false;
            twitterAuthorisationCodeLabel.Visible = false;
        }
    }
}
